package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.impl;

import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.MhFinishHouseArea;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.entity.MiddleHighRenovationRatio;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseFinishLossAssess.repository.MiddleHighRenovationRatioRepository;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023/8/7 10:56
 * @Description:
 */
@Repository
public class MiddleHighRenovationRatioRepositoryImpl implements MiddleHighRenovationRatioRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean save(MiddleHighRenovationRatio middleHighRenovationRatio) {
        String sql = "insert into xczh_middle_high_renovation_ratio values(:id,:xsemId,:bigCityConcreteValue,:bigCityMasonryValue,:mediumCityConcreteValue," +
                ":mediumCityMasonryValue,:smallCityConcreteValue,:smallCityMasonryValue,:createUser,:createTime,:updateUser,:updateTime,:type)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(middleHighRenovationRatio);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public boolean update(MiddleHighRenovationRatio middleHighRenovationRatio) {
        String sql = "update xczh_middle_high_renovation_ratio set big_city_concrete_value= :bigCityConcreteValue ,big_city_masonry_value = :bigCityMasonryValue ,medium_city_concrete_value = :mediumCityConcreteValue " +
                ",medium_city_masonry_value = :mediumCityMasonryValue ,small_city_concrete_value = :smallCityConcreteValue ,small_city_masonry_value = :smallCityMasonryValue "+
                ",update_user = :updateUser ,update_time = :updateTime where xsem_id = :xsemId and type = :type";
        SqlParameterSource source = new BeanPropertySqlParameterSource(middleHighRenovationRatio);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public MiddleHighRenovationRatio findByXsemId(String xsemId,String type) {
        String sql = "select * from xczh_middle_high_renovation_ratio where  xsem_id = ? and type = ? ";
        MiddleHighRenovationRatio middleHighRenovationRatio = new MiddleHighRenovationRatio();
        try {
            middleHighRenovationRatio = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MiddleHighRenovationRatio.class), xsemId,type);
            return middleHighRenovationRatio;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }


}
